package handler

import (
	"fmt"
	"time"

	_ "github.com/go-sql-driver/mysql"
	"github.com/xormplus/xorm"
)

var Engine *xorm.Engine

func ORM() {
	start := time.Now()
	var SQLerr error
	Engine, SQLerr = xorm.NewEngine("mysql", "root:okayokay@tcp(www.deeokay.top:3306)/SYY?charset=utf8")
	if SQLerr != nil {
		fmt.Println(SQLerr.Error())
	}
	objetArr := []interface{}{
		new(Admin),
		new(Staff),
		new(Email),
		new(News),
		new(Reply),
		new(Rc),
	}
	var finish= 0
	var channel= make(chan int)
	defer close(channel)
	for i, obj := range objetArr {
		go func(index int, object interface{}) {
			syncErr := Engine.Sync2(object)
			if syncErr != nil {
				fmt.Println(syncErr)
			} else {
				fmt.Printf("ORM同步第%d个模型完成%v\n", index, object)
			}
			finish++
			if finish == len(objetArr) {
				channel <- 0
			}
		}(i, obj)
	}
	<-channel
	fmt.Println("ORM用时:", time.Since(start))
	//SQL段结束
	//DayMission()
}